(function (w, d) {
    var hash = window.location.hash;
    if (hash.indexOf('#category')==0 || hash.indexOf('#/category')==0) {

        hash = hash.replace("#/", "#");
        hash = hash.split('/')
        if (hash.length > 2) {
            window.location.href = ('./category/' + hash[1] + '/');
        } 

    } else if (hash.indexOf('#content')==0 || hash.indexOf('#/content')==0) {

        hash = hash.replace("#/", "#");
        hash = hash.split('/')
        if (hash.length > 2) {
            window.location.href = ('./content/' + hash[1] + '/' + hash[2] + '/');
        } 

    } else {


    }
    var Correct = 'Correct';
    var offhelp = false;
    var setCorrect = function () {};
    w.onkeypress = function (e) {
        //console.log(e)
        offhelp = !offhelp;
        if (offhelp) {
            Correct = 'HelpCorrect'
        } else {
            Correct = 'Correct'
        }
        setCorrect();
    }

    w.StartTheExam = function (form) {
        var name = 'StartTheExam';
        var myApp = name + 'App';
        var myCtrl = name + 'Ctrl';
        var app = angular.module(myApp, []);
        var $_scope;
        var StartTimeValue = null;
        var TimeSpan = 1800;
        var getTimeString = function (LeftoverTime) {
            var minut = (LeftoverTime - LeftoverTime % 60) / 60;
            var sekot = LeftoverTime % 60;
            var ShowTime = minut + ":" + (sekot < 10 ? '0' : '') + sekot;
            return ShowTime;
        };

        var config = {
            StartBtn: 't-show',
            TimeForm: 't-hide',
            time: '30:00',
            AllTime: getTimeString(TimeSpan)
        };

        var StartTime = function () {
            if (StartTimeValue == null) {
                StartTimeValue = new Date();
            };
            var leave2 = new Date().getTime() - StartTimeValue.getTime();
            var leave3 = leave2 % (60 * 1000);
            leave3 = leave2;
            var seconds = Math.round(leave3 / 1000);
            var LeftoverTime = TimeSpan - seconds;
            if (LeftoverTime > 0) {
                $_scope.config.time = getTimeString(LeftoverTime);
                setTimeout(StartTime, 1000);
                $_scope.$apply();
            } else {
                $_scope.config.StartBtn = 't-hide';
                $_scope.config.TimeForm = 't-hide';
                $_scope.config.OkBtn = 't-show';
                $_scope.$apply();
            }
        };
        app.controller(myCtrl, function ($scope) {
            $scope.config = config;
            $_scope = $scope;
            $_scope.config.OkBtn = 't-hide';
            $_scope.Ok = function () {
                form.Ok();
                Fullscreen();
            };
            $_scope.Start = function () {
                $_scope.config.TimeForm = 't-show';
                $_scope.config.StartBtn = 't-hide';
                setTimeout(StartTime, 50);
                setTimeout(form.Show, 50);
                Fullscreen();
            };
        });

        var Fullscreen = function () {
            window.fullscre = document.webkitIsFullScreen;
            var docElm = document.documentElement;
            if (!window.fullscre) {
                if (docElm.requestFullscreen) {
                    docElm.requestFullscreen();
                } else if (docElm.mozRequestFullScreen) {
                    docElm.mozRequestFullScreen();
                } else if (docElm.webkitRequestFullScreen) {
                    docElm.webkitRequestFullScreen();
                } else if (docElm.msRequestFullscreen) {
                    docElm.msRequestFullscreen();
                }
            } else {
                if (document.exitFullscreen) {
                    document.exitFullscreen();
                } else if (document.mozCancelFullScreen) {
                    document.mozCancelFullScreen();
                } else if (document.webkitCancelFullScreen) {
                    document.webkitCancelFullScreen();
                } else if (document.msExitFullscreen) {
                    document.msExitFullscreen();
                }
            }
            window.fullscre = !window.fullscre;
        }
    };

    w.ExamIndexList = function (dan, duo, pan) {
        var name = 'ExamIndexList';
        var myApp = name + 'App';
        var myCtrl = name + 'Ctrl';
        var app = angular.module(myApp, []);
        var List = [];
        var index = 1;
        var getList = function (list, db) {
            for (var i = 0; i < db.length; i++) {
                db[i].index = index;
                db[i].ShowAnswer = Correct;
                index++;
                list.push(db[i]);
            }
            return list;
        };
        List = getList([], dan);
        List = getList(List, duo);
        List = getList(List, pan);
        app.controller(myCtrl, function ($scope) {
            $scope.List = List;
            $scope.active = "t-hide";
            $_scope = $scope;
            $_scope.SetItem = function (item) {
                active(item);
            }
            // $scope.$apply();
        });

        var active = function (item, old_item) {

            for (var i = 0; i < List.length; i++) {
                if (old_item != null && List[i]._id == old_item._id && old_item.Answer != null && old_item.Answer != "") {

                    List[i].Answer = old_item.Answer;

                };

                if (item != null && List[i]._id == item._id && item.Answer != null && item.Answer != "") {

                    List[i].Answer = item.Answer;

                };


                if (item != null && List[i]._id == item._id) {

                    if (item.Answer != null && item.Answer != "") {

                        List[i].Answer = item.Answer;

                    };

                    List[i].active = "primary";

                } else {

                    if (List[i].Answer != null && List[i].Answer != "") {

                        List[i].active = "success";

                    } else {

                        List[i].active = "default";

                    };

                };

            };

            for (var i = 0; i < self.NewItemPool.length; i++) {

                self.NewItemPool[i](item);

            };

        };

        this.Show = function (item, old_item) {

            $_scope.active = "t-show";

            active(item, old_item);

            $_scope.$apply();

        };

        this.NewItemPool = [];

        var self = this;

    };

    w.AnswerRoll = function (name, db, O, Type) {
        var off = false;
        var Index = 0,
            FormA, FormB;
        var form = document.getElementById(name);
        var myApp = name + 'App';
        var myCtrl = name + 'Ctrl';
        var _http;
        var app = angular.module(myApp, []);
        for (var i = 0; i < db.length; i++) {
            db[i].Index = i + 1;
        }
        var item = db[Index];

        var $_scope;

        app.controller(myCtrl, function ($scope, $http) {
            _http = $http;

            $scope.item = item;

            var cd = document.getElementsByName(Type);

            setvalue(cd, item.Answer, item);

            $_scope = $scope;

            $_scope.SetAnswer = function (o) {
                // console.log(o);
                if (o == 'All') {
                    o = "";
                    var cd = document.getElementsByName(Type);
                    for (var i = 0; i < cd.length; i++) {
                        if (cd[i].checked) {
                            o = o + cd[i].id.replace(Type + '_', '') + ","
                        };
                    };
                    var oj = o.split(',');
                    oj.pop();
                    o = oj.join(',')
                    db[Index].Answer = o;
                } else {
                    db[Index].Answer = o;
                };

                var data = {
                    id: db[Index]._id,
                    Answer: o
                };

                $http({
                    method: 'post',
                    url: './../Answer/',
                    data: data,
                }).success(function (req) {
                    setTimeout(function () {
                        db[Index].Answer = o;
                        if (ShowAnswer(o, db[Index])) {
                            self.Click(1)
                        } else {
                            self.Click(0);
                        };
                    }, 50);
                });
            };

            if (off == true) {
                self.Show();
            }
        });

        this.SetLinkForm = function (A, B) {
            FormA = A;
            FormB = B;
        };

        this.Show = function (ii) {
            form.className = form.className.replace('t-hide', 't-show');
            if (ii != 1) {
                O.Show(item);
            };
        };

        this.Hide = function () {
            form.className = form.className.replace('t-show', 't-hide');
        };

        var ShowAnswer = function (Answer, item) {
            if (Answer != null && item.Answer) {
                var Answer_A = Answer.split(',').sort().join(',');
                var Answer_B = item.Correct.split(',').sort().join(',');
                item.ShowAnswer = Correct;

                if (Answer_B == Answer_A) {
                    item.ShowAnswer = Correct;
                    return true;
                } else {
                    item.ShowAnswer = 'Show-Answer';
                    return false;
                }
            };
        };

        var setvalue = function (cd, Answer, item) {
            var p = Type;
            item.ShowAnswer = Correct;

            for (var i = 0; i < Math.min(4, cd.length); i++) {
                cd[i].checked = false;
                cd[i].disabled = false;
            };

            if (Answer == null) {
                return;
            } else {

            };

            for (var i = 0; i < Math.min(4, cd.length); i++) {
                cd[i].disabled = true;
            };

            var Answer_J = Answer.split(',').sort();

            ShowAnswer(Answer, item);

            for (var i = 0; i < Answer_J.length; i++) {
                if (Answer_J != '') {
                    document.getElementById(p + "_" + Answer_J[i]).checked = true;
                }
            };
        };

        this.Ok = function () {
            _http({
                method: 'post',
                url: './../Answer/ok/',
                data: {
                    id: window.TestId
                },
            }).success(function (req) {
                setTimeout(function () {
                    window.location.href = './../../train/';
                }, 50);
            });
        };
        var setItem = function (item) {
            px_Answer_E.checked = true;
            pz_Answer_E.checked = true;
            var Answer = item.Answer;
            Answer = Answer == null ? null : Answer;

            var cd = document.getElementsByName(Type);

            setvalue(cd, Answer, item);
            setCorrect = function () {
                $_scope.item.ShowAnswer = Correct;

                $_scope.$apply();
            };
            $_scope.item = item;
        };
        var old_item = null;
        this.Click = function (i) {
            Index = Index + i;
            if (Index < 0) {
                Index = 0;
                if (FormA != null) {
                    this.Hide();
                    FormA.Show();
                }
            } else if (Index > db.length - 1) {
                Index = db.length - 1;
                var item = db[Index];
                O.Show(item, old_item);
                Index = db.length - 1
                if (FormB != null) {
                    this.Hide();
                    FormB.Show();
                }
            } else {
                var item = db[Index];

                O.Show(item, old_item);

                setItem(item);

                old_item = item;
                $_scope.$apply();
            }
        };
        O.NewItemPool.push(function (item) {
            self.Hide();
            for (var i = 0; i < db.length; i++) {
                if (db[i]._id == item._id) {
                    item = db[i];
                    Index = i;
                    setItem(item);
                    self.Show(1);
                    return;
                } else {

                }
            }
        });
        var self = this;
    };
}(window, document))